/**
 * ThreeColumnFileInput - Reads in an input file that has 4 items formated as 
 * <item name>, <item mass>,<price per unit mass>.
 * 
 * CS575 Software Design
 * Group 6
 * Group Members: Anthony Davidson, Kendra Scheesley, Lyndsey Whitman, Stephen Zakrewsky
 * Date: May 19, 2012
 * Main Author(s): Anthony Davidson
 * 
 * Modified: 6/1 Stephen Zakrewsky
 */
package edu.drexel.cs575.a3;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ThreeColumnFileInput implements UserInputCollector {

	private Scanner scanner;

	ThreeColumnFileInput(String filename) throws FileNotFoundException {
		scanner = new Scanner(new File(filename));
	}

	public boolean hasNextItem() {
		if (this.scanner == null) {
			return false;
		}

		return scanner.hasNextLine();
	}

	public WeighableItem getNextItem() {
		if (!this.hasNextItem()) {
			throw new IllegalStateException("No items available");
		}

		String itemLine = this.scanner.nextLine();
		String[] itemArray = itemLine.split(",");

		WeighableItem weighableItem = null;

		if (itemArray.length == 3) {
			// For the purposes of this assignment we'll assume the data in the
			// text
			// file is formatted correctly.
			weighableItem = new SingleWeighableItem(itemArray[0],
					Double.parseDouble(itemArray[1]),
					Double.parseDouble(itemArray[2]));
		} else {
			throw new IllegalStateException(
					"File Format incorrect. File must contain 3 items comma separated.\n"
							+ "Example: <item name>, <item mass>,<price per unit mass>");

		}

		return weighableItem;
	}

}
